<?php
/**
 * Created by PhpStorm.
 * User: lance
 * Date: 2017/11/1
 * Time: 16:36
 */

namespace app\log\admin;


use app\admin\controller\Admin;
use think\Db;
use app\common\builder\ZBuilder;

/**
 *客户经理日志
 * Class Manager
 * @package app\log\admin
 */
class Manager extends Common
{
    public function showAddLog()
    {
        //领导能看所有客户经理添加日志
        $a_where = [
            'a.role_type' => 2,
            'a.action' => ['lt',5]
        ];
        $a_where = $this->getMap() + $a_where;
        $a_field = [
            'department', 'name', 'sex', 'number', 'action', 'time', 'status', 'a.id id'
        ];
        $s_tips = <<<EOT
1.操作栏，可查看更新客户经理的操作人员、以及更新客户经理的信息(更新成功的客户经理会显示最新的客户经理信息，失败时只会显示最初添加客户经理的信息)
EOT;

        $a_log = Db::name('role_log a')->field($a_field)->join('manager b', 'a.role_id=b.id', 'LEFT')->where($a_where)->order('time desc')->paginate();
        return ZBuilder::make('table')
            ->hideCheckbox()
            ->setSearch([
                'b.department' => '部门',
                'b.name' => '姓名',
                'b.number' => '柜员号',
            ])
            ->setPageTips($s_tips)
            ->addFilter('status', ['失败', '成功'])
            ->setTableName('role_log')
            ->addTimeFilter('time')
            ->addColumns(config('manager_log'))
            ->addTrClass('danger', 'status', 0)
            ->addTrClass('success', 'status', 1)
            ->setRowList($a_log)
            ->addRightButton('custom',
                [
                    'title' => '查看详情',
                    'icon' => 'glyphicon glyphicon-eye-open',
                    'href' => url('showadddetail', [
                        'id' => '__id__'
                    ]),
                ], true)
            ->fetch();
    }
    public function showUpdateLog(){
        //领导能看所有客户经理添加日志
        $a_where = [
            'a.role_type' => 2,
            'a.action' => ['egt',5]
        ];
        $a_where = $this->getMap() + $a_where;
        $a_field = [
            'department', 'name', 'sex', 'number', 'action', 'time', 'status', 'a.id id'
        ];
        $s_tips = <<<EOT
1.操作栏，可查看新增客户经理的操作人员、以及新增客户经理的信息(添加成功的客户经理会显示最新的客户经理信息，失败时只会显示最初添加客户经理的信息)
EOT;

        $a_log = Db::name('role_log a')->field($a_field)->join('manager b', 'a.role_id=b.id', 'LEFT')->where($a_where)->order('time desc')->paginate();
        return ZBuilder::make('table')
            ->hideCheckbox()
            ->setSearch([
                'b.department' => '部门',
                'b.name' => '姓名',
                'b.number' => '柜员号',
            ])
            ->setPageTips($s_tips)
            ->addFilter('status', ['失败', '成功'])
            ->setTableName('role_log')
            ->addTimeFilter('time')
            ->addColumns(config('manager_log'))
            ->addTrClass('danger', 'status', 0)
            ->addTrClass('success', 'status', 1)
            ->setRowList($a_log)
            ->addRightButton('custom',
                [
                    'title' => '查看详情',
                    'icon' => 'glyphicon glyphicon-eye-open',
                    'href' => url('showadddetail', [
                        'id' => '__id__'
                    ]),
                ], true)
            ->fetch();
    }
    /**
     * 客户经理新增日志
     */
    function showAddDetail()
    {
        $a_log = Db::name('role_log')->where('id', input('id/d', 0))->find();
        //描述
        $a_operate = Db::name('admin_user')->where('id', $a_log['operate_id'])->find();
        $a_role = Db::name('admin_role')->where('id', $a_operate['role'])->find();
        $a_content = json_decode($a_log['content'], true);
        //客户经理信息
        $a_manager = $a_log['status'] ? Db::name('manager')->where('id', $a_log['role_id'])->find() : $a_content;
        $i_id = $a_log['status'] ? $a_manager['id'] : '';
        $a_log['status'] = $a_log['status'] ? '<span style="color:green">成功</span>' : '<span style="color:red">失败</span>';
        $s_message = "操作时间：" . date('Y-m-d H:i:s', $a_log['time'])
            . '<br>状态：' . $a_log['status']
            . '<br>操作人员：' . $a_operate['username'] . '(' . $a_role['name'] . ')'
            . '<br>描述：' . config('manager_log')[5][4][$a_log['action']]
            . '<br>错误信息：' . $a_log['error'];
        $s_manager = 'ID：' . $i_id
            . '<br>部门：' . $a_manager['department'] . '<br>性别：' . $a_manager['sex']
            . '<br>姓名：' . $a_manager['name'] . '<br>性别：' . $a_manager['sex']
            . '<br>柜员号：' . $a_manager['number'] . '<br>证件号码：' . $a_manager['cert_number']
            . '<br>联系电话：' . $a_manager['tel_number']
            . '<br>办公室电话：' . $a_manager['office_number']
            . '<br>电子邮箱：' . $a_manager['email'];

        return $this->fetch('',
            [
                'manager' => $s_manager,
                'message' => $s_message
            ]);
    }
}